﻿using XNAFight.Scene.Core;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using XNAFight.Players;
using System;
using System.Collections.Generic;
using XNAFight.Engine;
using XNAFight.Characters.Core;

namespace XNAFight.Scene.SandBoxOne
{
    class DebugScreenOne : Screen
    {
        private Vector2 left_text_pos;
        private Vector2 right_text_pos;
        private Vector2 right_text_origin;
        private Player player;
        string left_text;
        List<string> right_text;
        private List<List<BMove>> last_moves;
        private string previous_move;
        float font_height;
        int nb_fdash;
        int nb_bdash;

        public DebugScreenOne(Player _player)
            : base()
        {
            left_text = "";
            right_text = new List<string>();
            player = _player;
            nb_fdash = 0;
            nb_bdash = 0;

            last_moves = new List<List<BMove>>();
        }

        protected override void LoadContent()
        {
            base.LoadContent();
            
            font_height = CommonContent.SmallFont.MeasureString("A").Y;

            left_text_pos = new Vector2(5, 0);
            right_text_pos = new Vector2(0, 0);
            right_text_origin = new Vector2(0, 0);
        }

        public override void Reset()
        {            
        }

        public override void Update(GameTime _game_time)
        {
            base.Update(_game_time);

            left_text = "Cur. state : " + player.Personnage.State.ToString() + "\n";
            left_text += "Animation : " + player.Personnage.AnimationEnCours + "\n";
            left_text += "Texture : " + player.Personnage.getAnimationData_from_AnimationName(player.Personnage.AnimationEnCours).NomTexture + "\n";
            left_text += "Cur. frame : " + (player.Personnage.Sprite.CurrentFrame).ToString("000") + "\n";
            left_text += "Total frm : " + player.Personnage.Sprite.NbFrames.ToString("000") + "\n";
            left_text += "Start frm : " + player.Personnage.Sprite.StartFrame.ToString("000") + "\n";
            left_text += "Loop : " + player.Personnage.Sprite.Loop + "\n";
            left_text += "MOVE : ";

            List<BMoveSequence> moves_buffer = MovesInterpreter.getMovesFromBuffer(player.Personnage.Sequences, player.Personnage.Buffer.LastBMoves);
            List<BMoveSequence> moves_flags = MovesInterpreter.getMovesFromFlags(moves_buffer, player.Personnage.PositionVerticale);
            BMoveSequence priority_move = MovesInterpreter.getPriorityMove(moves_flags);

            if (priority_move != null)
                previous_move = priority_move.Coup.Name;

            left_text += previous_move + "\n";

            if (MovesInterpreter.isDashingForward(player.Personnage.Buffer.LastBMoves))
                nb_fdash++;
            if (MovesInterpreter.isDashingBackward(player.Personnage.Buffer.LastBMoves))
                nb_bdash++;

            left_text += "FDashs : " + nb_fdash + "\n";
            left_text += "BDashs : " + nb_bdash + "\n";

            right_text.Clear();
            right_text.Add("BUFFER ");
            
            if (player.Personnage.Buffer.LastBMoves[0].Count > 0)
            {
                last_moves.Insert(0, player.Personnage.Buffer.LastBMoves[0]);
                if (last_moves.Count > 29)
                    last_moves.RemoveAt(last_moves.Count - 1);
            }

            string temp;
            for (int i = 0; i < last_moves.Count; i++)
            {
                temp = "";
                for (int j = 0; j < last_moves[i].Count; j++)
                {
                    temp += last_moves[i][j].ToString() + " ";
                }
                right_text.Add(temp);
            }
        }

        public override void Draw(GameTime gameTime)
        {
            base.Draw(gameTime);

            CommonContent.SpriteBatch.Begin();

            CommonContent.SpriteBatch.DrawString(CommonContent.SmallFont, left_text, left_text_pos, Color.Black);

            right_text_pos.X = GraphicsDevice.Viewport.Width;
            right_text_pos.Y = 0;
            for (int i = 0; i < right_text.Count; i++)
            {
                right_text_origin.X = CommonContent.SmallFont.MeasureString(right_text[i]).X;
                CommonContent.SpriteBatch.DrawString(CommonContent.SmallFont, right_text[i], right_text_pos, Color.Black, 0.0f, right_text_origin, 1.0f, SpriteEffects.None, 0.5f);
                right_text_pos.Y += font_height;
            }

            CommonContent.SpriteBatch.End();
        }

        public override void HandleInputs()
        {

        }
    }
}
